package com.qj.platform.pay.core.dao;

import com.qj.base.commons.utils.CodeGenerator;
import com.qj.base.commons.utils.SqlUtil;
import com.qj.platform.pay.core.domain.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hezhijie on 2016/11/17.
 */
@Repository
public class UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<User> queryList(String id,String userCode,String name,String machineCode,int pageNum,int pageSize){
        List<Object> argList=new ArrayList<>();
        StringBuilder sql=new StringBuilder("select * from sys_user u ");
        if(StringUtils.isNotEmpty(machineCode)){
            sql.append(",user_machine um where u.code=um.user_code ");
        }else{
            sql.append(" where 1=1 ");
        }
        SqlUtil.addEqualWhereParam(sql,argList,"u.id",id);
        SqlUtil.addEqualWhereParam(sql,argList,"u.code",userCode);
        SqlUtil.addEqualWhereParam(sql,argList,"um.machine_code",machineCode);
        SqlUtil.addLikeWhereParam(sql, argList, "u.name", name);
        sql.append(" order by u.code ");
        SqlUtil.addPostgrePagination(sql,argList,pageNum,pageSize);
        return jdbcTemplate.query(sql.toString(), argList.toArray(new Object[]{}), new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet resultSet, int i) throws SQLException {
                User user=new User();
                user.setId(resultSet.getString("id"));
                user.setName(resultSet.getString("name"));
                user.setUserCode(resultSet.getString("code"));
                user.setDeptName(resultSet.getString("dept_name"));
                user.setFirstLoginTime(resultSet.getTimestamp("first_login_time"));
                user.setLatestLoginTime(resultSet.getTimestamp("latest_login_time"));
                return user;
            }
        });
    }

    public void updateUser(User user){
        StringBuilder sql=new StringBuilder("update sys_user set latest_login_time=now() ");
        List<Object> argList=new ArrayList<>();
        sql.append("where code=?");
        argList.add(user.getUserCode());
        jdbcTemplate.update(sql.toString(),argList.toArray(new Object[]{}));
    }

    public void updateAllDisable(){
        jdbcTemplate.update("update sys_user set status=0");
    }

    public void deleteDisabledUser(){
        jdbcTemplate.update("delete from sys_user where status!=1");
    }


    public void insertUserMachine(String userCode,String machineCode){
        String sql = "insert into user_machine (id,user_code,machine_code,create_time) values(?,?,?,now()) ";
        jdbcTemplate.update(sql, CodeGenerator.getUUID32(),userCode,machineCode);
    }
}
